package com.getpebble.android.jskit.bridge;

import android.os.Bundle;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.framework.appmessage.AppMessage;
import com.getpebble.android.framework.appmessage.AppMessage.AppMessageCommand;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.notification.PblNotificationProcessor;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.LegacyNotification;
import com.getpebble.android.notifications.model.PblNotification.Source;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class DeviceBridge {
    private static DeviceBridge sInstance;

    public static DeviceBridge getInstance() {
        if (sInstance == null) {
            sInstance = new DeviceBridge();
        }
        return sInstance;
    }

    public boolean isConnected() {
        List<PblDeviceRecord> connectedDevices = getConnectedDevices();
        boolean connected = (connectedDevices == null || connectedDevices.isEmpty()) ? false : true;
        Trace.verbose("DeviceBridge", "JSKit thinks device is connected: " + connected);
        return connected;
    }

    public String getCurrentAddress() {
        List<PblDeviceRecord> connectedDevices = getConnectedDevices();
        if (connectedDevices == null || connectedDevices.isEmpty()) {
            return null;
        }
        return ((PblDeviceRecord) connectedDevices.get(0)).pblDevice.getAddress();
    }

    protected List<PblDeviceRecord> getConnectedDevices() {
        List<PblDeviceRecord> connectedDevices = new ArrayList();
        PblDeviceRecord connectedDeviceRecord = PebbleApplication.getConnectedDeviceRecord();
        if (connectedDeviceRecord != null) {
            connectedDevices.add(connectedDeviceRecord);
        }
        return connectedDevices;
    }

    public boolean postQueryRunningApps() {
        Trace.verbose("DeviceBridge", "sending js app fetch request");
        return sendRequestToAllConnectedDevices(new EndpointRequest(EndpointId.APP_LIFECYCLE, EndpointAction.REQUEST_RUNNING_APP));
    }

    public boolean postStartAppWithUUID(UUID appId) {
        Trace.verbose("DeviceBridge", "sending app start request");
        Bundle args = new Bundle();
        args.putString(EndpointArgumentKeys.UUID.toString(), appId.toString());
        return sendRequestToAllConnectedDevices(new EndpointRequest(EndpointId.APP_LIFECYCLE, EndpointAction.START_APP, args));
    }

    public boolean postAppMessage(AppMessage message) {
        Trace.verbose("DeviceBridge", "sending js app message");
        Bundle args = new Bundle();
        args.putParcelable(EndpointArgumentKeys.APP_MESSAGE.toString(), message);
        return sendRequestToAllConnectedDevices(new EndpointRequest(EndpointId.APP_MESSAGE, EndpointAction.PUSH_APP_MESSAGE, args));
    }

    public void postSimpleNotification(String title, String body, String timeStamp) {
        LegacyNotification content = new LegacyNotification();
        content.title = title;
        content.body = body;
        PblNotificationProcessor.processNotification(PblNotification.from(content, Source.JSKIT));
    }

    public boolean postAck(int transactionId) {
        Trace.verbose("DeviceBridge", "sending JS app message ack");
        return postAppMessage(new AppMessage((byte) (transactionId & 255), null, AppMessageCommand.ACK, null));
    }

    private boolean sendRequestToAllConnectedDevices(EndpointRequest request) {
        boolean success = true;
        List<PblDeviceRecord> connectedDeviceRecords = getConnectedDevices();
        if (connectedDeviceRecords.isEmpty()) {
            return false;
        }
        for (PblDeviceRecord deviceRecord : connectedDeviceRecords) {
            MessageRouter messageRouter = MessageRouter.getRouter(deviceRecord.pblDevice);
            if (messageRouter == null) {
                Trace.debug("DeviceBridge", "sendRequestToAllConnectedDevices: messageRouter is null");
                success = false;
            } else {
                success = success && messageRouter.onRequest(request, null);
            }
        }
        return success;
    }
}
